package cn.cht.controller;

import cn.cht.client.EmailClient;
import cn.cht.pojo.AuthCode;
import cn.cht.service.AuthCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/code")
public class CodeController {

    public static final Logger log = LoggerFactory.getLogger(CodeController.class);

    @Autowired
    private AuthCodeService authCodeService;
    @Autowired
    private EmailClient emailClient;

    @GetMapping("create/{email}")
    public boolean create(@PathVariable String email) {
        AuthCode code = authCodeService.create(email);
        log.info("email: {}, code: {}", email, code);
        return emailClient.SendEmail(email, code.getCode());
    }

    @GetMapping("validate/{email}/{code}")
    public Integer validate(@PathVariable String email, @PathVariable String code) {
        return authCodeService.validate(email, code);
    }

}
